//
//  HLHomeListImageView.m
//  YueYue
//
//  Created by hulu001 on 2023/5/25.
//

#import "HLHomeListImageView.h"
#import "HLPickerManager.h"

@interface HLHomeListImageView ()

@property (nonatomic, strong) QMUIButton *firstView;
@property (nonatomic, strong) QMUIButton *secondView;
@property (nonatomic, strong) QMUIButton *thirdView;
@property (nonatomic, strong) QMUIGridView *imageContentView;

@end

@implementation HLHomeListImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        self.firstView = [[QMUIButton alloc] init];
        _firstView.imageView.image = [HLUIHelper avatarPlaceholder];
        _firstView.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _firstView.tag = HLCommonTagA + 0;
        _firstView.layer.masksToBounds = YES;
        _firstView.layer.cornerRadius = 4;
        [_firstView addTarget:self action:@selector(previewAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.secondView = [[QMUIButton alloc] init];
        _secondView.imageView.image = [HLUIHelper avatarPlaceholder];
        _secondView.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _secondView.tag = HLCommonTagA + 1;
        _secondView.layer.masksToBounds = YES;
        _secondView.layer.cornerRadius = 4;
        [_secondView addTarget:self action:@selector(previewAction:) forControlEvents:UIControlEventTouchUpInside];

        
        self.thirdView = [[QMUIButton alloc] init];
        _thirdView.imageView.image = [HLUIHelper avatarPlaceholder];
        _thirdView.imageView.contentMode = UIViewContentModeScaleAspectFill;
        _thirdView.tag = HLCommonTagA + 2;
        _thirdView.layer.masksToBounds = YES;
        _thirdView.layer.cornerRadius = 4;
        [_thirdView addTarget:self action:@selector(previewAction:) forControlEvents:UIControlEventTouchUpInside];
        
        
        CGFloat width = (SCREEN_WIDTH - 40) / 3;
        self.imageContentView = [[QMUIGridView alloc] init];
        _imageContentView.columnCount = 3;
        _imageContentView.rowHeight = width;
        _imageContentView.separatorWidth = 4;
        _imageContentView.separatorColor = UIColorWhite;
        _imageContentView.separatorDashed = NO;
        [self addSubview:_imageContentView];
        [_imageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.bottom.mas_equalTo(0);
        }];

//        self.numLabel = [[QMUILabel alloc] init];
//        _numLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
//        _numLabel.textColor = UIColorWhite;
//        _numLabel.text = @"6张";
//        _numLabel.backgroundColor = HEXAColor(0x000000, .6);
//        _numLabel.textAlignment = NSTextAlignmentCenter;
//        _numLabel.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8);
//        _numLabel.layer.masksToBounds = YES;
//        _numLabel.layer.cornerRadius = 10;
//        [self addSubview:_numLabel];
//        [_numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(20);
//            make.right.bottom.mas_equalTo(-2);
//        }];
        
    }
    return self;
}


- (void)setOpus:(NSArray<HLOpusModel *> *)opus
{
    _opus = opus;
//    NSInteger count = opus.count;
//    _numLabel.hidden = count < 4;
//    _numLabel.text = [NSString stringWithFormat:@"%ld张", count];
    [_imageContentView qmui_removeAllSubviews];
    NSArray *imageViews = @[_firstView, _secondView, _thirdView];
    
    [opus enumerateObjectsUsingBlock:^(HLOpusModel * _Nonnull opus, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx >= 3) {
            return;
        }
        QMUIButton *imageView = imageViews[idx];
        [imageView sd_setImageWithURL:[NSURL URLWithString:opus.opusLink] forState:UIControlStateNormal placeholderImage:UIImageMake(@"image_placeholder")];
        [_imageContentView addSubview:imageView];
    }];
    
}

- (void)previewAction:(QMUIButton *)btn
{
    NSInteger index = btn.tag - HLCommonTagA;
    
    HXPhotoManager *photoManager = [HXPhotoManager managerWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
    
    NSMutableArray *models = [NSMutableArray arrayWithCapacity:_opus.count];
    for (HLOpusModel *opus in _opus) {
        HXCustomAssetModel *model = [HXCustomAssetModel assetWithNetworkImageURL:[NSURL URLWithString:opus.opusLink] selected:YES];
        [models addObject:model];
    }
    [photoManager addCustomAssetModel:models];
    [HLPickerManager previewWithManager:photoManager index:index controller:self.qmui_viewController];
}


@end
